<?php
/**
 * realizeaza transformarea documentelor din xml in pdf
 */
require 'fpdf.php';

class pdfPrinter extends FPDF {
	/* -----proprietati---- */

	/* -----metode---- */

	/**
	 * constructor
	 */
	public function __construct(){
		define('FPDF_FONTPATH','include/font/');
		parent::FPDF();
		$this->SetDisplayMode('real');
		//margini
		$this->SetTopMargin(25);
		$this->SetLeftMargin(30);
		$this->SetRightMargin(25);

		$this->AliasNbPages();
		$this->AddPage();
		//$this->SetFont('Times', '', 12);
		//echo "OKay";
	}

	public function printParagraph($text){
		$this->SetFont('Times', '', 12);
		$spaces="            ";
		$this->MultiCell(0,5,$spaces.$text,0);
		$this->Ln(4);
	}

	public function printNote($text){
		$this->SetFont('Times', '', 12);
		$spaces="      ";
		$this->Cell(16);
		$this->MultiCell(125,5,$spaces."Note: \n \n".$text,0);
		$this->Ln(4);
	}

	public function printAttention($text){
		$this->SetFont('Times', '', 12);
		$spaces="      ";
		$this->Cell(16);
		$this->MultiCell(125,5,$spaces."Attention! \n \n".$text,0);
		$this->Ln(4);
	}

	public function printQuote($text){
		$this->SetFont('Times', 'I', 12);
		$spaces="      ";
		$this->Cell(5);
		$this->MultiCell(145,5,$spaces."\" ".$text." \"",0);
		$this->Ln(4);
	}

	public function printImage($path, $caption){
		$pos=$this->fh-$this->GetY()-25;
		$x=($this->fw-$this->lMargin-$this->rMargin-72/2)/2;
		//var_dump($x);
		if($pos > 72){
			$this->Image($path, $x, $this->GetY());
			$this->Ln(70);
			$this->MultiCell(0,5,$caption,0,"C");
			$this->Ln(4);
		}else{
			$this->AddPage();
			$this->Image($path, $x, $this->GetY());
			$this->Ln(70);
			$this->MultiCell(0,5,$caption,0,"C");
			$this->Ln(4);
		}
	}

	public function printTable($header, $data, $info){
		$ws=array();
		$nrCols=count($header);
		foreach ($header as $h){
			$ws[]=$this->GetStringWidth($h)+4;
		}
		foreach ($data as $row){
			for($i=0; $i<$nrCols; $i++){
				if($this->GetStringWidth($row[$i])>$ws[$i]){
					$ws[$i]=$this->GetStringWidth($row[$i])+4;
				}
			}
		}
		$x=($this->fw-$this->lMargin-$this->rMargin-array_sum($ws))/2;
		//var_dump($x);
		$minWidth=45;
		if($x <= $minWidth){
			//cauta stringul cel mai lung
			$col=0;
			$line=0;
			$max=0;
			for($i=0; $i<count($data); $i++){
				for($j=0; $j<$nrCols; $j++){
					$length=$this->GetStringWidth($data[$i][$j]);
					if($length > $max){
						$col=$j;
						$line=$i;
						$max=$length;
					}
				}
			}
			//echo $col."-".$line;
			
			//sparge celula
			$maxStringLength=round($this->GetStringWidth($data[$line][$col]));
			$pieces=ceil($maxStringLength/$minWidth);
			//echo $pieces."->".$maxStringLength;
			$chunklen=ceil(strlen($data[$line][$col])/$pieces);
			//echo "--".$chunklen;
			$splitArray=str_split($data[$line][$col], $chunklen);
			//var_dump($splitArray);
			//setam noua dim. a celulei
			$ws[$col]=$this->GetStringWidth($splitArray[0])+4;
			//centrare tabel
			$x=($this->fw-$this->lMargin-$this->rMargin-array_sum($ws))/2;
			
			//creare array suplimentar
			$extraArray=array();
			for($i=0; $i<count($splitArray); $i++){
				for($j=0; $j<$nrCols; $j++){
					//tratare prima linie ce contine restul valorilor
					if($j == $col){
						$extraArray[$i][$j]=$splitArray[$i];
					}else{
						if($i == 0){
							$extraArray[$i][$j]=$data[$line][$j];
						}else{
							$extraArray[$i][$j]="";
						}
					}
				}
			}
			//var_dump($extraArray);
			//echo "<br /><br />";
			
			//inserare array suplimentar in $data
			//var_dump($data);
			//echo "<br /><br />";
			$endArray=array();
			$initLength=count($data);
			for($i=$line; $i<$initLength; $i++){
				//echo "i:".count($data)."<br />";
				$endArray[$i-$line]=$data[$i];
				unset($data[$i]);
			}
			//var_dump($data);
			//echo "<br /><br />";
			//scoate linia sparta
			array_shift($endArray);
			//var_dump($endArray);
			//echo "<br /><br />";
			foreach ($extraArray as $e){
				array_push(&$data, $e);
			}
			foreach ($endArray as $e){
				array_push(&$data, $e);
			}
			//var_dump($data);
			
		}
		//echo $col."-".$line;

		//header
		$this->Cell($x);
		for($i=0; $i<$nrCols; $i++){
			$this->Cell($ws[$i], 5, $header[$i], 1, 0, "C");
		}
		$this->Ln();
		//data
		foreach ($data as $row){
			$this->Cell($x);
			for($i=0; $i<$nrCols; $i++){
				if(is_numeric($row[$i])){
					$this->Cell($ws[$i], 5, number_format($row[$i]), "LR", 0,'R');
				}else{
					$this->Cell($ws[$i], 5, $row[$i],"LR");
				}
			}
			$this->Ln();
		}
		//final line
		$this->Cell($x);
		$this->Cell(array_sum($ws), 0, '', "T");
		$this->Ln(4);
		
		//info
		$this->MultiCell(0,5,$info,0,'C');
		$this->Ln(4);		
	}

	public function printTitle($title)
	{
		$this->SetFont('Times','',14);
		//$spaces="             ";
		$this->Cell(0,10,$title,0,0,'C');
		$this->Ln(15);
	}



	//Page header
	/*	public function Header()
	 {
		//Logo
		//$this->Image('logo_pb.png',10,8,33);
		//Arial bold 15
		$this->SetFont('Arial','',8);
		//Move to the right
		//$this->Cell(80);
		//Title
		$this->SetY(5);
		//$this->Cell(80);
		$h=$this->MultiCell(0,10,'..::Dante v0.3::.. Group Document Creation and Review ::Test Page::',0,'R');
		//Line break
		$posX=$this->GetX();
		$posY=$this->GetY();
		//$this->Ln(1);
		$this->Line($posX, $posY, $posX+155, $posY);
		$this->Ln(20);
		}*/

	//Page footer
	public function Footer()
	{
		$this->SetY(-15);
		$this->SetFont('Times', '', 12);
		$this->Cell(0,10,'- '.$this->PageNo().' -',0,0,'C');
	}
}
?>